<?php

namespace app\index\controller;

use app\common\model\NodeSon;
use app\common\model\UserTypeSon;
use app\common\model\ManagerDuty as ManagerDutyModel;


class ManagerDuty extends Base
{
    /**
     * [getIndex  角色列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $user = $this->user;
        $map = [];
        $this->getPageAndSize($param);
        $model = new ManagerDutyModel;
        try{
            $map['comid'] = $user['comid'];
            $map['status'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size);
            $moren = $model->limit(['comid'=>0],0,9);
            array_unshift($lists,$moren);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看职务列表');
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getAll  所有职务列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getAll()
    {
        $user = $this->user;
        $map = [];
        $model = new ManagerDutyModel;
        try{
            $map['comid'] = $user['comid'];
            $map['status'] = 1;
            $data = $model->getAllData($map,'id','id,duty');
            $moren = $model->limit(['comid'=>0],0,9);
            array_unshift($data,$moren);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'获取所有职务');
        return admin_json(1,'获取成功',$data);
    }
    /**
     * [giveAccess 分配权限]
     * @return [type] [description]
     * @author [C。]
     */
    public function getAccess()
    {
        $param = input('param.');
        $user = $this->user;
        $node = new NodeSon();
        //获取现在的权限
        $nodeStr = $node->getNode($param['id']);
        writelog($user['mid'],$user['comid'],'查看角色' . $param['id'] . '权限');

        return admin_json(1, 'success', $nodeStr);
    }
    /**
     * [giveAccess 分配权限]
     * @return [type] [description]
     * @author [C。]
     */
    public function postAccess()
    {
        $param = input('param.');
        $user = $this->user;
        $doparam = [
            'id' => $param['id'],
            'rules' => $param['rule']
        ];
        $user1 = new UserTypeSon();
        $res = $user1->editAccess($doparam);
        writelog($user['mid'],$user['comid'],'给角色'.$param['id'].'分限权限');

        return admin_json(1,'角色权限分配成功');
    }
    /**
     * [postAdd  添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ManagerDutyModel;
        try{
            $param['comid'] = $user['comid'];
            $id = $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'添加职务'.$id);
        return admin_json(1,'添加成功');
    }
    /**
     * [postEdit  修改]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ManagerDutyModel;
        try{
            $id = $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'修改职务'.$id);
        return admin_json(1,'修改成功');
    }
    /**
     * [getDel  删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getDel()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ManagerDutyModel;
        try{
            $model->deleteFalseData(['id'=>$param['id']],['status'=>-1]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'删除职务'.$param['id']);
        return admin_json(1,'删除职务成功');
    }
}